// To show the icon as indicator
const St = imports.gi.St;
const Gio = imports.gi.Gio;
const Panel = imports.ui.panel;
const PopupMenu = imports.ui.popupMenu;

function PM25Indicator(extensionMeta, pm25) {
	log ("PM25Indicator init");
	this._init.apply(this, [extensionMeta, pm25]);
};

PM25Indicator.prototype = {
	pm25: null,
	icons: {},

	_init: function(extensionMeta, pm25) {
		log ("PM25Indicator _init");
		this.extensionMeta = extensionMeta;
		this.pm25 = pm25;

		let icons_paths = {
			na:     this.extensionMeta.path + "/icons/pm25-na.png",
			green:  this.extensionMeta.path + "/icons/pm25-green.png",
			yellow: this.extensionMeta.path + "/icons/pm25-yellow.png",
			orange: this.extensionMeta.path + "/icons/pm25-orange.png",
			red:    this.extensionMeta.path + "/icons/pm25-red.png",
			purple: this.extensionMeta.path + "/icons/pm25-purple.png",
			maroon: this.extensionMeta.path + "/icons/pm25-maroon.png"
		}

		for (let p in icons_paths) {
			let icon = Gio.icon_new_for_string(icons_paths[p]);
			this.icons[p] = icon;
		}

		this.actor = new St.BoxLayout({ style_class: "pm25-indicator" });
		this.icon = new St.Icon({
			icon_type: St.IconType.SYMBOLIC,
			gicon: this.icons.na,
			icon_size: Panel.PANEL_ICON_SIZE
		});

		this.actor.add_actor(this.icon);

		this.menu = new PopupMenu.PopupMenuItem("PM25", {
		 	reactive: false,
		 	style_class: "pm25"});

		this.update ();
		log ("PM25Indicator init end");
	},

	update: function() {
		log ('PM25Indicator update');
		this.icon.gicon = this.icons[this.pm25.get_current_pm25_range()];
		this.menu.label.set_text("Beijing PM2.5:\n" + this.pm25.get_current_pm25());
		log ('PM25Indicator update end');
	}
}
